import 'dart:async';
import 'package:flutter/material.dart';

import 'my_list_element.dart';
// 列表聚合上拉加载下拉刷新

class MyListView extends StatefulWidget {
  // 传入参数
  const MyListView({
    Key? key,
    required this.dataForm,// 数据列表[{},{}……]
    this.frontHeight = 50.0, // 前置高度，避开搜索栏
    required this.pullup, // 上拉回调函数
    required this.dropdown,// 下拉回调函数
    required this.onPressed,// 点击回调函数
    required this.onTap,// 点击回调函数
    this.title = 'title',
    this.code = 'code',
    this.desc = 'descShow',
    this.imgpath = 'imgPath',
    this.itemid = 'id',
    this.tag = 'tag',
    this.title1 = '随便反正最后还是用变量',
    this.desc1 = 'place',
    this.title2 = '店平方',
    this.desc2 = 'square',
    this.title3 = '负责人',
    this.desc3 = 'manage_name',
    this.belong = '',
    this.icon = Icons.more_horiz,
    // required this.text,
  }) : super(key: key,);
  final List dataForm;
  final double frontHeight;
  final ValueChanged<Map> onPressed;
  final ValueChanged<int> onTap;
  final VoidCallback pullup;
  final VoidCallback dropdown;//ValueChanged dropdown;//
  final String title;
  final String code;
  final String desc;
  final String imgpath;
  final String itemid;
  final String tag;
  final String title1;
  final String title2;
  final String title3;
  final String desc1;
  final String desc2;
  final String desc3;
  final String belong;
  final IconData icon;
  // final Text text;

  @override
  State<StatefulWidget> createState() => MyListViewState();
}

// 构建
class MyListViewState extends State<MyListView> {
  // 上拉加载回调函数
  final ScrollController _controller = ScrollController();
  // 上拉加载更多
  _loadMore() async {
    /// 强制休眠 1 秒
    await Future.delayed(Duration(seconds: 1));
    /// 放入到集合中
    setState(() {widget.pullup();});
    setState((){});
    // setState(() {_DataRestart();});
  }

  @override
  void initState() {
    /// 为滚动控制器添加监听
    _controller.addListener(() {
      // var maxScroll = _controller.position.maxScrollExtent;
      // var pixel = _controller.position.pixels;

      // 在加载的时候锁死
      if (_controller.position.pixels ==
          _controller.position.maxScrollExtent) {
        /// 触发上拉加载更多机制
        _loadMore();
      }
    });
    setState((){});
    super.initState();
  }
  @override
  void dispose() {
    // 销毁 滚动控制器 ScrollController
    _controller.dispose();
    super.dispose();
  }

  makeList() {
    List<Widget> theList = [];
    theList = [
      SizedBox(height: widget.frontHeight,),// 前置高度
    ];
    List<String> choices = ['OK','NG'];
    Map choicesValues = {'OK':'合格','NG':'不合格'};
    for(var item in widget.dataForm) {
      theList.add(MyListElement(
          text:  item[widget.tag].toString()=='失效中'?Text(
              '激活',style: TextStyle(
                  fontSize: 10,
                  color: Colors.lightBlueAccent
                ),
          ):Text(
              '失效',style: TextStyle(
                  fontSize: 10,
                  color: Colors.redAccent//Colors.lightBlueAccent
              ),
          ),
          icon: item[widget.tag].toString()=='失效中'?Icons.volunteer_activism:Icons.warning,
          title:item[widget.title].toString(),
          code:item[widget.code]==''?'':item[widget.code].toString(),
          belong: item[widget.belong].toString(),
          color: Colors.black,
          titleFontWeight:FontWeight.w500,// FontWeight.bold,18,
          titleSize:15,
          // desc:item[widget.desc],// 'The msg is the Desc !',
          dcolor:Colors.black38,// Colors.black38,
          imgpath:item[widget.imgpath].toString()==''|| item[widget.imgpath].toString()=='null'?'':item[widget.imgpath].toString(),
          id:item[widget.itemid],
          onPressed:(){widget.onPressed(item);},// (){MyToast.success2(item['id'].toString());},
          onTap:(){widget.onTap(item[widget.itemid]);},// onTap: (){MyToast.success2('点击' + item['id'].toString());},
          tag: item[widget.tag].toString(),
          tagColor: item[widget.tag].toString()=='失效中'?Colors.redAccent:Colors.amber,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                child: Text.rich(
                    TextSpan(
                        children: [
                          TextSpan( text: widget.title1,style: TextStyle(fontSize:10,color: Color.fromARGB(255,255,97,0),backgroundColor: Color.fromARGB(255,255,226,209)),),
                          TextSpan( text: '  ', style: TextStyle(fontSize:10,color: Colors.black26),),
                          TextSpan( text: choices.contains(item[widget.desc1].toString()) ?choicesValues[item[widget.desc1].toString()]:' ',
                            style: item[widget.desc1].toString()=='NG'?TextStyle(
                                fontSize:10,
                                backgroundColor:Colors.redAccent,
                                color: item[widget.desc1].toString()=='NG'?Colors.yellow:Colors.black87
                            ):TextStyle(
                                fontSize:10,
                                color: item[widget.desc1].toString()=='NG'?Colors.yellow:Colors.black87
                            )
                          ),
                          TextSpan( text: '  |  ', style: TextStyle(fontSize:10,color: Colors.black26),),
                          TextSpan( text: widget.title2,style: TextStyle(fontSize:10,color: Color.fromARGB(255,4,100,240),backgroundColor: Color.fromARGB(255,218,240,255)),),
                          TextSpan( text: '  ', style: TextStyle(fontSize:10,color: Colors.black26),),
                          TextSpan( text: item[widget.desc2].toString(),style: TextStyle(fontSize:10,color: Colors.black87),),
                        ]
                    )
                ),
                // padding: EdgeInsets.fromLTRB(8, 8, 8, 8),
              ),
              SizedBox(height: 5,),
              Container(
                child: Text.rich(
                    TextSpan(
                        children: [
                          TextSpan( text: widget.title3,style: TextStyle(fontSize:10,color: Color.fromARGB(255,0,177,40),backgroundColor: Color.fromARGB(255,231,255,231)),),
                          TextSpan( text: '  ', style: TextStyle(fontSize:10,color: Colors.black26),),
                          TextSpan( text: ' '+ item[widget.desc3].toString() +' ',style: TextStyle(fontSize:10,color: Colors.black87),),
                        ]
                    )
                ),
                // padding: EdgeInsets.fromLTRB(8, 8, 8, 8),
              ),
              SizedBox(height: 5,),
            ],
          )

        // SizedBox(height: 1,)// "place" "square" "manage_name"
      ));
    }
    theList.add(SizedBox(height: 50,));
    return theList;
  }

  Future<void> _onRefresh() async {
    await Future.delayed(Duration(seconds: 1));// 强制休眠 1 秒
    setState(() {widget.dropdown();});
    return;
  }

  @override
  Widget build(BuildContext context) {

    return RefreshIndicator(
      color: Colors.blue,//圆圈进度颜色
      displacement: 50.0,//下拉停止的距离
      backgroundColor: Colors.grey[200],//背景颜色
      onRefresh: _onRefresh,
      child: ListView(
        controller: _controller,
        children: makeList(),
      ),
    );
  }

}